#encoding=utf8
import os
import fnmatch
from win32com import client as wc
from win32com.client import Dispatch

def wps2txt(filePath,savePath=''):
	dirs,fileName = os.path.split(filePath)

	#获取切分后的文件后缀（小写）
	fileType = os.path.splitext(fileName)[-1].lower()
	newName = checkAndNewName(fileName,fileType)

	if newName is None:
		print('not doc,docs or pdf files')
		return

	if savePath == '':
		savePath = dirs

	savePath = os.path.join(savePath,newName)

	wpsApp = wc.Dispatch('KWPS.Application')
	print(filePath)
	txt = wpsApp.Documents.Open(filePath)

	txt.SaveAs(savePath,4)
	txt.Close()


def checkAndNewName(fileName,fileType):
	if fileType == '.doc' or fileType == '.docx' or fileType == '.pdf':
		if fnmatch.fnmatch(fileName,'*.doc') or fnmatch.fnmatch(fileName,'*.pdf'):
			return fileName[:-3] + 'txt'
		elif fnmatch.fnmatch(fileName,'*.docx'):
			return fileName[:-4] + 'txt'
		else:
			return
	else:
		return


if __name__ == '__main__':
	test_dir = 'F:\python project\KNN\\testFiles\BinStream直播中控操作指南CNv4.pdf'
	wps2txt(test_dir)